package cn.ubeta.b2c;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import cn.ubeta.b2c.R;
import cn.ubeta.b2c.common.AnalyseActivity;
import cn.ubeta.b2c.utils.AppConfig;

/**
 * Created by Ruily on 16/1/13.
 */

public class GuideActivity extends AnalyseActivity {

    public final static String IDENTIFY = "1.0";

    private static int GUIDE_RES_BG[] = {R.drawable.launch_bg_1, R.drawable.launch_bg_1, R.drawable.launch_bg_1, R.drawable.launch_bg_1};
    private static int GUIDE_RES_ICON[] = {R.drawable.launch_icon_1, R.drawable.launch_icon_1, R.drawable.launch_icon_1, R.drawable.launch_icon_1};
    private static int GUIDE_RES_DESC[] = {R.string.launch_desc_1, R.string.launch_desc_1, R.string.launch_desc_1, R.string.launch_desc_1};

    private ViewPager mViewpager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //getWindow().setBackgroundDrawable(null);
        setContentView(R.layout.activity_guide);

        mViewpager = (ViewPager) findViewById(R.id.vp_guide);
        mViewpager.setAdapter(new ViewPagerAdapter(this));
        mViewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                if (position == GUIDE_RES_BG.length - 1 && positionOffset >= 0.25F) {
                    enterHome(GuideActivity.this);
                }
            }

            @Override
            public void onPageSelected(int position) {
                // 刷新当前页面标识
                ImageView ivs[] = {(ImageView) findViewById(R.id.iv_guide_ctrl1), (ImageView) findViewById(R.id.iv_guide_ctrl2), (ImageView) findViewById(R.id.iv_guide_ctrl3), (ImageView) findViewById(R.id.iv_guide_ctrl4)};
                for (int i = 0; i < GUIDE_RES_BG.length; ++i) {
                    int res;
                    if (position == i) {
                        res = R.drawable.launch_pagectrl_current;
                    } else {
                        res = R.drawable.launch_pagectrl_normal;
                    }
                    ivs[i].setImageResource(res);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }

    private static class ViewPagerAdapter extends PagerAdapter {

        private Activity mActivity;

        public ViewPagerAdapter(Activity activity) {
            this.mActivity = activity;
        }

        @Override
        public int getCount() {
            return GUIDE_RES_BG.length + 1;
        }

        // 来判断显示的是否是同一张图片，这里我们将两个参数相比较返回即可
        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }

        // PagerAdapter只缓存三张要显示的图片，如果滑动的图片超出了缓存的范围，就会调用这个方法，将图片销毁
        @Override
        public void destroyItem(ViewGroup view, int position, Object object) {
            view.removeView((View) object);
        }

        // 当要显示的图片可以进行缓存的时候，会调用这个方法进行显示图片的初始化，我们将要显示的ImageView加入到ViewGroup中，然后作为返回值返回即可
        @Override
        public View instantiateItem(ViewGroup container, int position) {
            View ll = LayoutInflater.from(container.getContext()).inflate(R.layout.item_guide, null);
            if (position < GUIDE_RES_BG.length) {
                ll.setBackgroundResource(GUIDE_RES_BG[position]);
                ll.findViewById(R.id.iv_guide_icon).setBackgroundResource(GUIDE_RES_ICON[position]);
                ((TextView) ll.findViewById(R.id.tv_guide_desc)).setText(GUIDE_RES_DESC[position]);
            }
            if (position == GUIDE_RES_BG.length - 1) {
                ll.findViewById(R.id.tv_guide_enter).setVisibility(View.VISIBLE);
                ll.findViewById(R.id.tv_guide_enter).setOnClickListener(view -> enterHome(mActivity));
            }
            container.addView(ll, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            return ll;
        }
    }

    /**
     * 进入主页
     */
    private static void enterHome(Activity activity) {
        if (activity != null) {
            AppConfig.setRunGuide();
            activity.finish();
            activity.overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        AppConfig.setRunGuide();
    }

    /**
     * 跳转开机引导
     * @param context
     */
    public static void gotoGuide(Activity context) {
        Intent intent = new Intent(context, GuideActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
        context.overridePendingTransition(R.anim.in_right_to_left, android.R.anim.fade_out);
    }
}
